<?php

namespace App\Http\Controllers;

use Laravel\Lumen\Routing\Controller as BaseController;
use Illuminate\Support\Facades\Redis;

class Controller extends BaseController
{

    public function parseRequest() {

    }

    public static function now() {
        return intval(microtime(true) * 1000);
    }

    public static function sameDay($t) {
        return date('Y-m-d') == date('Y-m-d', $t) ? true : false;
    }

    public static function todayZero() {
        return strtotime(date('Y-m-d 00:00:00'));
    }

    public static function nowInTimeRange($range) {
        $range_arr = explode('-', $range);
        $start_time = strtotime(date('Y-m-d '.$range_arr[0].':00'));
        $end_time = strtotime(date('Y-m-d '.$range_arr[1].':59'));
        $now = time();
        return $start_time <= $now && $now <= $end_time ? true : false;
    }

    public function result($data=[], $msg='', $reason='') {
        $base_json = config('common.JsonResp');
        $base_json['code']  = 0;
        $base_json['data'] = $data ? $data : null;
        $base_json['msg']  = $msg;
        $base_json['reason'] = $reason;
        $base_json['time'] = self::now();
        $reddot = false;
        $uid = \Auth::user();
        if(!empty($uid) ) {
            if(Redis::GET('TaskReddot:'.$uid)) $reddot = true;
        }
        $base_json['reddot'] = $reddot;
        return response()->json($base_json);
    }

    public function error($msg='', $reason='', $res=1, $data=[]) {
        $base_json = config('common.JsonResp');
        $base_json['code']  = $res;
        $base_json['data'] = $data ? $data : null;
        $base_json['msg']  = $msg;
        $base_json['reason'] = $reason;
        $base_json['time'] = self::now();
        return response()->json($base_json);
    }

    public function throwError($error_name, $reason='') {
        $base_json = config('common.JsonResp');
        $err_cfg = config('error.cfg.'.$error_name);
        $base_json['code']  = $err_cfg['code'];
        $base_json['data'] = null;
        $base_json['msg']  = $err_cfg['msg'];
        $base_json['reason'] = $reason;
        $base_json['time'] = self::now();
        return response()->json($base_json);
    }
}
